package com.dushuren.project.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.dushuren.project.mapper.TeamMapper;
import com.dushuren.project.model.entity.Team;
import com.dushuren.project.model.enums.SuggestSearchSelectEnum;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

@SpringBootTest
public class addTable {

  @Resource
  TeamService teamService;

  @Resource
  UserService userService;

  @Resource
  UserTeamService userTeamService;

  @Resource
  TeamMapper teamMapper;

  /**
   * 测试连接池
   * 9958
   *7391
   *
   * 9941
   * 9904
   * 7611
   */
  @Test
  void pool() {
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
//	QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//	queryWrapper.select("id","userStatus", "creditsNum","userAccount", "userName", "avatarUrl", "gender", "userProfile", "phone", "email", "tags");
	for (int i = 0; i< 10000; i++) {
	  teamService.getById(i);
	}
	stopWatch.stop();
	System.out.println(stopWatch.getTotalTimeMillis());
  }


  @Autowired
  DataSource dataSource;

  @Test
  void contextLoads() {
	// 查看数据源
	System.out.println(dataSource.getClass());
	// 获取数据库连接
	try {
	  System.out.println(dataSource.getClass());
	  //获得连接
	  Connection connection =   dataSource.getConnection();
	  System.out.println(connection);
	  DruidDataSource druidDataSource = (DruidDataSource) dataSource;
	  System.out.println("druidDataSource 数据源最大连接数：" + druidDataSource.getMaxActive());
	  System.out.println("druidDataSource 数据源初始化连接数：" + druidDataSource.getInitialSize());
	  // 关闭连接
	  connection.close();
	} catch (SQLException e) {
	  e.printStackTrace();
	}
  }



  // CUP 密集型：分配的核心线程数 = CPU  -1
  // IO 密集型： 分配的核心线程数可以大于CUP 核数
  private ExecutorService executorService = new ThreadPoolExecutor(60, 1000, 10000, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(10000));


  ///List<CompletableFuture<Void>> futures = new ArrayList<>();
  //	for (int i = 0; i < 40; i++) {
  //	  List<User> userList = new ArrayList<>();
  //	  while(true){
  //		j++;
  //		User user = new User();
  //		user.setUsername("假用户");
  //		user.setUserAccount("scholarlazy");
  //		user.setAvatarUrl("https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/9226/1.jpg");
  //		user.setUserPassword("12345678");
  //		user.setGender(0);
  //		user.setPhone("123");
  //		user.setEmail("123");
  //		user.setUserStatus(0);
  //		user.setUserRole(0);
  //		user.setTags("");
  //		user.setPlanetCode("1111111");
  //		userList.add(user);
  //		if (j % batchSize == 0) {
  //		  break;
  //		}
  //	  }
  //	  // 异步执行需要注意，执行的顺序无所谓，不要用到非并发类的集合
  //	  CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
  //		System.out.println("threadName" + Thread.currentThread().getName());
  //		userService.saveBatch(userList, batchSize);
  //	  }, executorService);
  //	  futures.add(future);
  //	}
  //	CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();
  //	stopWatch.stop();
  //	System.out.println(stopWatch.getTotalTimeMillis());
  /***
   * 使用并发插入数据 10 万条数据
   */
//  @Test
  void team1(){
	// 分组10组
	int batchSize = 5000;
	int j = 0;
	List<CompletableFuture<Void>> list = new ArrayList<>();
	for (int i=0; i< 20;i++) {
	  List<Team> userList = new ArrayList<>();
	  while (true) {
		j++;
		// 给team表添加测试数据
		Team team = new Team();
		team.setTeamName("假数据");
		team.setTeamDescription("假数据阿萨大大大阿松大速度啊大大啊的大事大");
		team.setMaxNum(4);
		team.setTeamType("小程序");
		team.setTeamTechnologyType("[\"Python\",\"Java\"]");
		team.setUserId(0L);
		team.setTeamApplyState(1);
		userList.add(team);
		if (j % batchSize == 0) {
		  break;
		}
	  }
	  // 异步执行需要注意，执行顺序无所谓，不要用到非并非类的集合
	  CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
		System.out.println("theadName" + Thread.currentThread().getName());
		teamService.saveBatch(userList, batchSize);
	  }, executorService);
	  list.add(future);
	}
	CompletableFuture.allOf(list.toArray(new CompletableFuture[]{})).join();
  }

  @Test
   void selectEnum() {
	String[] strings = SuggestSearchSelectEnum.getValues().toArray(new String[0]);
	System.out.println(Arrays.toString(strings));
  }

}
